package service;

import com.alibaba.fastjson.JSON;
import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import service.SmsService;

import java.util.HashMap;

public class SmsServiceImpl implements SmsService {
    // 生产环境请求地址:app.cloopen.com
    private String serviceIp = "app.cloopen.com";
    // 请求端口
    private String servicePort = "8883";
    // 主账号,登录云通讯网站后,可在控制台看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
    private String accountSId = "8a216da880d67afb0180fa4e743d05d8";
    private String accountToken = "e7a3d608500143598c5f6384a4bdc567";
    // 请使用管理控制台中已创建应用的APPID
    private String appId = "8a216da880d67afb0180fa4e753605df";
    @Override
    public boolean sendMsg(String to, String templateId, String... data) throws Exception {
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serviceIp,servicePort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        HashMap<String,Object> result = sdk.sendTemplateSMS(to,templateId,data);
        if ("000000".equals(result.get("statusCode"))){
            return true;
        }else{
            System.out.println(JSON.toJSONString(result));
        }
        return false;
    }
}
